
import * as Constants from '../../../common/Constants';
import * as RegexUtils from '../../../utils/RegexUtils';
import * as NetworkUtils from '../../../http/NetworkUtils';
import PageRouter from '../../../utils/PageRouter';

import { $Toptips } from '../../../components/CompRouter';

var app = getApp();
Page({
    data: {
        loading: false,
    },

    onPwdInput: function (ee) {
        //密码特殊处理，不允许输入空格
        return ee.detail.value.trim();
    },

    onSubmit: function (e) {
        let newPwd1 = e.detail.value.newPwd1;
        let newPwd2 = e.detail.value.newPwd2;

        let errorMsg = this._checkForm(newPwd1, newPwd2);
        errorMsg && $Toptips.error({text: errorMsg});
        // this.setData({
        //     errorMsg: errorMsg
        // });

        // if (!errorMsg) {
        !errorMsg && this._requestChangePwd(newPwd2);
        // }
    },

    _checkForm: function (newPwd1, newPwd2) {
        if (newPwd1 !== newPwd2) {
            return "新密码两次输入不一致请修改";
        }

        return RegexUtils.regexTypeWithMsg(newPwd2, Constants.RegexTypes.NEW_LOGIN_PWD);
    },

    _requestChangePwd: function (newPwd) {
        let params = {
            agentId: app.globalData.userData.agentId,
            password: newPwd
        };
        NetworkUtils.buildRequest(Constants.Interfaces.FUNC_UPDATE_LOGIN_PWD, params, {
            doHttpSucess: (response) => {
                if (Constants.RetCodes.SUCCESS === response.retCode) {
                    PageRouter.navigateBack({
                        from: Constants.LoginFrom.MODIFY_PWD,
                        data: {}
                    });
                }
                return false;
            }
        }).send();
    }
});


